<div class="wrapper">

<div class="panel panel-default">

  <div class="panel-heading clearfix">

    <li class="name">商品名称</li>

  </div><!-- panel-heading -->

  <div class="panel-body">
    <?php if(!$shopcar){ ?>
    <li class="empty">购物车还是空空的！快去填满购物车吧！<a href="<?php echo base_url(); ?>" class="btn btn-primary">去购物</a></li>
    <?php }else{ ?>
    <?php foreach ($shopcar as $items): ?>
 	
 	<?php 
 	$book_id = substr($items['id'],4);
 	$book_info = $this->m_books->selectBookById($book_id);
 	?>
    <div class="lists">
      <li class="image"><img src="<?php echo base_url('uploads/thumb_50/'.$book_info['0']->book_img); ?>" width="50" /></li>
      <div class="rightname">
      	  <li class="tname"><a href="<?php echo base_url('lists/item/'.$book_id);?>" target="_blank"><?php echo $book_info['0']->book_name; ?></a></li>
	      <li class="shopcar">
	        <button class="btn btn-default l" data-bookid="<?php echo $book_id;?>">-</button>
	        <input type="text" class="form-control" data-bookid="<?php echo $book_id;?>" data-unitprice="<?php echo $items['price']; ?>" value="<?php echo $items['amount']; ?>" maxlength="3" />
	        <button  class="btn btn-default r" data-bookid="<?php echo $book_id;?>">+</button>
	      </li>
	      <li class="total">￥<?php echo $items['amount']*$items['price']; ?></li>
	      <li class="doit"><a href="Javascript::" data-goodsid="<?php echo $items['id'];?>" class="deleteShopCar btn btn-danger">删除</a></li>
	  </div>
    </div>

  <?php endforeach; ?>
  <?php }?>

  </div>
  
  <!-- footer -->
  <div class="panel-footer clearfix">
    <div class="brief clearfix">

    <li>应付金额<em class="price" id="total_price"><?php echo $total_price; ?></em></li>
    <li class="button"><a href="Javascript:;" class="btn btn-primary" id="goOrder">去结算</a></li>

    </div>
  </div>
  <!-- ./footer -->

</div>

</div><!-- wrapper -->

<script>

//decrease , button click
$(".shopcar").children('.l').click(function(){

	$("#goOrder").button("loading");
	$($(this).parent().children('.form-control')).popover('destroy');
	$(this).parent().removeClass('has-error');

	// decrease button click
	if(parseInt($(this).parent().children('.form-control').val()) <= 1)
	{
	   // when input val < 1 ,show error
	   $(this).parent().children('.form-control').parent().addClass('has-error');
	   $(this).parent().children('.form-control').attr('data-content','本商品一件起卖');
	   $(this).parent().children('.form-control').attr('data-placement','top');
	   $(this).parent().children('.form-control').popover('show');
	   $(this).parent().children('.form-control').val('1'); //set input val = 1
	   $("#goOrder").button("reset");
	}
	else
	{
	   var this_obj = $(this);
		
		$.ajax({
		       url:$("#baseUrl").val()+"shopcar/updateShopCar",
		       dataType:'json',
		       type:'post',
		       data:{"id":$(this).attr('data-bookid'),"type":"decrease"},
		       success:function(e){

		    	   $("#goOrder").button("reset");
		           if(e.status == '10602'){
		               this_obj.siblings(".form-control").parent().addClass('has-error');
		               this_obj.siblings(".form-control").attr('data-content','库存不足！%>_<%');
		               this_obj.siblings(".form-control").attr('data-placement','top');
		               this_obj.siblings(".form-control").popover('show');
		           }else if(e.status == '10000'){
						$("#total_amount").html(e.amount);
						$("#total_price").html(e.price);
						this_obj.siblings(".form-control").val(parseInt(this_obj.siblings(".form-control").val()) - 1);
						var unitamount = this_obj.siblings(".form-control").val();
						var unitprice = this_obj.siblings(".form-control").attr('data-unitprice');
						
						this_obj.parent().siblings(".total").html(parseInt(unitamount)*parseInt(unitprice));
			       }else if(e.status == '10603'){
			    	   this_obj.siblings(".form-control").parent().addClass('has-error');
		               this_obj.siblings(".form-control").attr('data-content','本商品一件起卖');
		               this_obj.siblings(".form-control").attr('data-placement','top');
		               this_obj.siblings(".form-control").popover('show');

			       }
			       

		       }
		
		   });
	}

});

//increase, click button
$(".shopcar").children('.r').click(function(){

	$("#goOrder").button("loading");
	
	// clear error
	$($(this).parent().children('.form-control')).popover('destroy');
	$(this).parent().removeClass('has-error');
	
	var this_obj = $(this);
	
	$.ajax({
	       url:$("#baseUrl").val()+"shopcar/updateShopCar",
	       dataType:'json',
	       type:'post',
	       data:{"id":$(this).attr('data-bookid'),"type":"increase"},
	       success:function(e){

	           if(e.status == '10602'){
	               this_obj.siblings(".form-control").parent().addClass('has-error');
	               this_obj.siblings(".form-control").attr('data-content','库存不足！%>_<%');
	               this_obj.siblings(".form-control").attr('data-placement','top');
	               this_obj.siblings(".form-control").popover('show');
	           }else if(e.status == '10000'){
					$("#total_amount").html(e.amount);
					$("#total_price").html(e.price);
					this_obj.siblings(".form-control").val(parseInt(this_obj.siblings(".form-control").val()) + 1);
					var unitamount = this_obj.siblings(".form-control").val();
					var unitprice = this_obj.siblings(".form-control").attr('data-unitprice');
					
					this_obj.parent().siblings(".total").html(parseInt(unitamount)*parseInt(unitprice));
					

		       }else if(e.status == '10603'){
		    	   this_obj.siblings(".form-control").parent().addClass('has-error');
	               this_obj.siblings(".form-control").attr('data-content','本商品一件起卖');
	               this_obj.siblings(".form-control").attr('data-placement','top');
	               this_obj.siblings(".form-control").popover('show');

		       }

	           $("#goOrder").button("reset");
		       

	       }
	
	   });

});

//clear error
$("input").focus(function(){
   
	$(this).popover('destroy');
	$(this).parent().removeClass('has-error');
});

//当用户输入的商品数量小于1,则提示用户，并强行设置数量为1
$(".shopcar").children('.form-control').blur(function(){

	$("#goOrder").button("loading");
	if( (parseInt($(this).val()) < 1) || ($(this).val() ==''))
	{
	   // when input val < 1 ,show error
	   $(this).parent().addClass('has-error');
	   $(this).attr('data-content','本商品一件起卖');
	   $(this).attr('data-placement','top');
	   $(this).popover('show');
	   $(this).val('1'); //set input val = 1 
	   $("#goOrder").button("reset");
	}else{

		var this_obj = $(this);

		$.ajax({
		       url:$("#baseUrl").val()+"shopcar/updateShopCar",
		       dataType:'json',
		       type:'post',
		       data:{"id":this_obj.attr('data-bookid'),"type":"userinput","amount":this_obj.val()},
		       success:function(e){

		           if(e.status == '10602'){
		               this_obj.parent().addClass('has-error');
		               this_obj.attr('data-content','库存不足！%>_<%');
		               this_obj.attr('data-placement','top');
		               this_obj.popover('show');
		               //this_obj.val('1');
		           }else if(e.status == '10000'){
						$("#total_amount").html(e.amount);
						$("#total_price").html(e.price);
						var unitamount = this_obj.val();
						var unitprice = this_obj.attr('data-unitprice');
						
						this_obj.parent().siblings(".total").html(parseInt(unitamount)*parseInt(unitprice));
						

			       }else if(e.status == '10603'){
			    	   this_obj.parent().addClass('has-error');
		               this_obj.attr('data-content','本商品一件起卖');
		               this_obj.attr('data-placement','top');
		               this_obj.popover('show');
		               this_obj.val('1');

			       }

		           $("#goOrder").button("reset");
			       

		       }
		
		   });
	}

});

//清除弹出的错误
function clearError(id){
	$(id).popover('destroy');
	$(id).parent().removeClass('has-error');
}

//删除单件的物品
$(".deleteShopCar").click(function(){

	var suk_id = $(this).attr('data-goodsid');
	var this_obj = $(this);
	$.ajax({
        url:"<?php echo base_url(); ?>shopcar/deleteShopcar/"+suk_id,
        type:"post",
        dataType:"json",
        cache:false,
        success:function(e){
            if(e.success==1){
            	this_obj.parent().parent().hide();
            	$("#total_amount").html(e.amount);
            	$("#total_price").html(e.price);
            }else if(e.success==2){
            	$(".panel-body").html('<li class="empty">购物车还是空空的！快去填满购物车吧！<a href="<?php echo base_url(); ?>" class="btn btn-primary">去购物</a></li>');
            	$("#total_amount").html('0');
            	$("#total_price").html('0');

            }
        }
            
    });

});


//点击按钮检测过后去结算
$("#goOrder").click(function(){
	$.ajax({
		url:$("#baseUrl").val()+"shopcar/checkShopCar",
		dataType:"json",
		type:"post",
		success:function(e){

			if(e.status == '10601'){
				alertError('购物车还是空的，不能去结算哦！');
			}else if(e.status=='10000'){
				location.href=$("#baseUrl").val()+"shopcar/order";
			}else if(e.status=='10602'){
				alertError("“"+e.name+'”&nbsp;库存不足');
			}
			
		}
		
	});
});
</script>



<!-- shopCarAddress -->
<div class="modal fade" id="shopCarAddress" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title">修改送货地址</h4>
      </div>
      <div class="modal-body">
	      <div class="alert alert-info">
			<li>登录后，可以保存多个送货地址！并有惊喜相送！</li>
		  </div>
        <div class="form-group">
	      		
	      		<label>配送地址</label>
	      		<input type="text" class="form-control" name="" />
	      	
	      	</div><!-- form-group -->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">保存送货地址</button>
      </div>
    </div>
  </div>
</div>